﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Prism.Core.Net.Tcp
{
    /// <summary>
    /// 进度化连接类型。
    /// </summary>
    public class ProgressableConnection : Connection
    {
        /// <summary>
        /// 初始化进度化连接的新实例。
        /// </summary>
        /// <param name="target">目标。</param>
        public ProgressableConnection(ConnectionTarget target)
            : base(target)
        {
        }

        /// <summary>
        /// 初始化进度化连接的新实例。
        /// </summary>
        /// <param name="tcpClient">Tcp客户端。</param>
        internal ProgressableConnection(TcpClient tcpClient)
            : base(tcpClient)
        {

        }

        /// <summary>
        /// 初始化进度化连接的新实例。
        /// </summary>
        /// <param name="baseConnection">基类实例。</param>
        public ProgressableConnection(Connection baseConnection)
            : base(baseConnection.TcpClient)
        {

        }

        void ProgressableNetworkHelper_ConnectionFailed(object sender, EventArgs e)
        {
            base.OnConnectionFailed(new EventArgs());
        }

        protected override void OnNetworkHelperInitialization(ConnectedCallback callback)
        {
            this.OnProgressableNetworkHelperInitialization(callback);
        }

        protected override void OnNetworkHelperInitialization()
        {
            this.OnProgressableNetworkHelperInitialization();
        }

        protected virtual void OnProgressableNetworkHelperInitialization(ConnectedCallback callback)
        {
            this.ProgressableNetworkHelper = new ProgressableNetworkHelper(this.TcpClient);
            this.ProgressableNetworkHelper.ConnectionFailed += new EventHandler(ProgressableNetworkHelper_ConnectionFailed);
            this.ProgressableNetworkHelper.WriteDataProgress += new EventHandler<DataProgressEventArgs>(ProgressableNetworkHelper_WriteDataProgress);
            this.ProgressableNetworkHelper.ReadDataProgress += new EventHandler<DataProgressEventArgs>(ProgressableNetworkHelper_ReadDataProgress);
            this.ProgressableNetworkHelper.WriteDataSpeed += new EventHandler<DataSpeedEventArgs>(ProgressableNetworkHelper_WriteDataSpeed);
            this.ProgressableNetworkHelper.ReadDataSpeed += new EventHandler<DataSpeedEventArgs>(ProgressableNetworkHelper_ReadDataSpeed);
            callback(true);
        }

        void ProgressableNetworkHelper_ReadDataSpeed(object sender, DataSpeedEventArgs e)
        {
            this.OnReadDataSpeed(e);
        }

        void ProgressableNetworkHelper_WriteDataSpeed(object sender, DataSpeedEventArgs e)
        {
            this.OnWriteDataSpeed(e);
        }

        protected virtual void OnProgressableNetworkHelperInitialization()
        {
            this.ProgressableNetworkHelper = new ProgressableNetworkHelper(this.TcpClient);
            this.ProgressableNetworkHelper.ConnectionFailed += new EventHandler(ProgressableNetworkHelper_ConnectionFailed);
            this.ProgressableNetworkHelper.WriteDataProgress += new EventHandler<DataProgressEventArgs>(ProgressableNetworkHelper_WriteDataProgress);
            this.ProgressableNetworkHelper.ReadDataProgress += new EventHandler<DataProgressEventArgs>(ProgressableNetworkHelper_ReadDataProgress);
            this.ProgressableNetworkHelper.WriteDataSpeed += new EventHandler<DataSpeedEventArgs>(ProgressableNetworkHelper_WriteDataSpeed);
            this.ProgressableNetworkHelper.ReadDataSpeed += new EventHandler<DataSpeedEventArgs>(ProgressableNetworkHelper_ReadDataSpeed);
        }

        void ProgressableNetworkHelper_ReadDataProgress(object sender, DataProgressEventArgs e)
        {
            this.OnReadDataProgress(e);
        }

        void ProgressableNetworkHelper_WriteDataProgress(object sender, DataProgressEventArgs e)
        {
            this.OnWriteDataProgress(e);
        }

        protected override void OnNetworkHelperBeginWrite(object data, DataSentCallback callback)
        {
            this.OnProgressableNetworkHelperBeginWrite(data, callback);
        }

        protected virtual void OnProgressableNetworkHelperBeginWrite(object data, DataSentCallback callback)
        {
            this.ProgressableNetworkHelper.BeginWrite(data, new WriteDataEndCallback(delegate(bool completed)
            {
                base.OnWriteDataEndCallback(completed, callback);
            }));
        }

        protected override void OnNetworkHelperBeginRead()
        {
            this.OnProgressableNetworkHelperBeginRead();
        }

        protected virtual void OnProgressableNetworkHelperBeginRead()
        {
            this.ProgressableNetworkHelper.BeginRead(new ReadDataEndCallback(delegate(object data)
            {
                base.OnReadDataEndCallback(data);
            }));
        }

        protected override void OnNetworkHelperStopRead()
        {
            this.OnProgressableNetworkHelperStopRead();
        }

        protected virtual void OnProgressableNetworkHelperStopRead()
        {
            this.ProgressableNetworkHelper.StopRead();
        }

        private ProgressableNetworkHelper ProgressableNetworkHelper;

        public event EventHandler<DataSpeedEventArgs> WriteDataSpeed;

        protected void OnWriteDataSpeed(DataSpeedEventArgs e)
        {
            if (this.WriteDataSpeed != null) { this.WriteDataSpeed(this, e); }
        }

        public event EventHandler<DataSpeedEventArgs> ReadDataSpeed;

        protected void OnReadDataSpeed(DataSpeedEventArgs e)
        {
            if (this.ReadDataSpeed != null) { this.ReadDataSpeed(this, e); }
        }

        /// <summary>
        /// 当正在写入数据的时候引发的处理过程事件。
        /// </summary>
        public event EventHandler<DataProgressEventArgs> WriteDataProgress;

        /// <summary>
        /// 当正在接收数据的时候引发的处理过程事件。
        /// </summary>
        public event EventHandler<DataProgressEventArgs> ReadDataProgress;

        protected void OnWriteDataProgress(DataProgressEventArgs e)
        {
            if (this.WriteDataProgress != null) { this.WriteDataProgress(this, e); }
        }

        protected void OnReadDataProgress(DataProgressEventArgs e)
        {
            if (this.ReadDataProgress != null) { this.ReadDataProgress(this, e); }
        }

        public override void Dispose()
        {
            base.Dispose();
            this.ProgressableNetworkHelper.Dispose();
        }
    }
}
